/*
 *	CHANGE LOG
 *
 *  01.03.07
 *  
 *      11/30/2009 - DW - Closed the data readers when connections are made to the db. 
 *
*/

using System;
using System.Data;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Services.Exceptions;

namespace ITCrossing
{
    public partial class itcMetaPost_Settings : ModuleSettingsBase
    {

        /// <summary>
        /// handles the loading of the module setting for this
        /// control
        /// </summary>
        public override void LoadSettings()
        {
            try
            {
                if (!IsPostBack)
                {
					
                    // Retrieve the upload URL from the modules settings
                    string sSQL =
						"SELECT * FROM {databaseOwner}{objectQualifier}itcMetaPost (nolock) WHERE ItemId = 1";
                    string sURL = null;
                	string allowedUploadExtensions = null;
                    using (IDataReader dr =
                        (IDataReader)DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(sSQL))
                    {
                        while (dr.Read())
                        {
                            sURL = dr["URL"].ToString() + "";
                            allowedUploadExtensions = dr["AllowedUploadExtensions"].ToString();
                        }
                    }

                    if (sURL != null)
                    {
                        txtUploadUrl.Text = sURL;
                    }
					if (!string.IsNullOrEmpty(allowedUploadExtensions)) {
						txtAllowedUploadExtensions.Text = allowedUploadExtensions; }
					else
					{
						txtAllowedUploadExtensions.Text = "jpg,zip,rar,gif,png";
					}
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }

        /// <summary>
        /// handles updating the module settings for this control
        /// </summary>
        public override void UpdateSettings()
        {
            try
            {
                if (!string.IsNullOrEmpty(txtUploadUrl.Text))
                {
                    // Use DAL+ to update the module table.
                    string sSQL = "UPDATE {databaseOwner}{objectQualifier}itcMetaPost SET URL = '" +
								  txtUploadUrl.Text.Replace("'", "''") + "', AllowedUploadExtensions = '" +
								  txtAllowedUploadExtensions.Text.Replace("'", "''").Replace(".","") + 
								  "' WHERE ItemId = 1";
                    DotNetNuke.Data.DataProvider.Instance().ExecuteSQL(sSQL);
                }
            }
            catch (Exception ex)
            {
                Exceptions.ProcessModuleLoadException(this, ex);
            }
        }
    }
}